package com.bc.web.controller;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

import com.bc.web.config.R;
import com.fs.common.extend.FSController;
import com.fs.common.util.FSDate;
import com.fs.common.util.FSHttp;
import com.github.wxpay.sdk.WXPayUtil;
import com.github.wxpay.sdk.WXPayXmlUtil;

public class Qqueryexchagerate extends FSController {
	/*
	 * GBP: Great Britain pound---英镑
HKD: Hong Kong dollar---港币
USD: United States dollar---美元
JPY: Japanese yen---日元
CAD: Canadian dollar---加元
AUD: Australian dollar---澳元
EUR: Euro---欧元
NZD: New Zealand dol---新西兰元
KRW: South Korean won---韩元
THB: Thailand baht---泰珠
SGD: Singapore dollar---新加坡元
RUB: Russia ruble---俄罗斯币
	 */
	public void index() {
		render("queryrate.html");
	}
	
	public void queryReate() throws Exception {
		String fee_type = getPara("fee_type", "HKD");
		TreeMap<String, String> map = new TreeMap<String, String>();
		map.put("appid", R.wxgj_appid);
		map.put("mch_id", R.wxgj_merchant_no);
		map.put("sub_mch_id", "253447240");
		map.put("fee_type", fee_type);
		map.put("date", (FSDate.getDate()).replace("-", ""));
		map.put("sign", WXPayUtil.generateSignature(map, R.wxgj_merchant_ser));
		
		String xml = WXPayUtil.mapToXml(map);
		String res = FSHttp.postString("https://api.mch.weixin.qq.com/pay/queryexchagerate", xml);
		Map<String, String> resMap = WXPayUtil.xmlToMap(res);
		String return_code = resMap.get("return_code");
		String return_msg = resMap.get("return_msg");
		if("SUCCESS".equals(return_code)) {
			String rate_time = resMap.get("rate_time");
			String rate = resMap.get("rate");
			String g_fee_type = resMap.get("fee_type");
			
			BigDecimal bRate = new BigDecimal(rate);
			BigDecimal divisor = new BigDecimal("100000000");
			String sRate= bRate.divide(divisor).toString();
			
			renderHtml("备注:外币兑换RMB的比例乘以10的8次方即为此值，例如美元兑换人民币的比例为6.5，则rate=650000000"+
			"<br><br><br><br>"+		
		"日期:"+rate_time + "<br><br>币种:" + g_fee_type + "兑换为人民币<br><br><br><br>" +
			"微信返回格式是:" + rate + "<br><br>"
				+ "<h1>计算后汇率是" + sRate +
		"</h1>");
			
		}else {
			renderHtml(return_code + ":" + return_msg);
		}
		
	}
	
}
